package com.yunji.order.command.order.impl;

import com.yunji.order.command.order.AbsOrderCommand;
import com.yunji.order.domain.OrderDO;
import com.yunji.order.dto.cmd.OrderCancelCommandDTO;
import com.yunji.order.emuns.OrderStatusEnum;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;

/**
 * @author : peter-zhu
 * @date : 2025/3/19 10:18
 * @description : TODO
 **/
@Component
public class OrderCancelCommand extends AbsOrderCommand<OrderCancelCommandDTO, Boolean> {
    @Override
    public Boolean execute(OrderCancelCommandDTO cmd) {
        OrderDO orderDO = cmd.getOrderDO();
        OrderDO update = new OrderDO();
        update.setOrderId(orderDO.getOrderId());
        update.setClosedTime(new Date());
        update.setClosedType(cmd.getCloseTypeEnum().getCode());
        update.setOrderStatus(OrderStatusEnum.ORDER_CANCEL.getCode());
        boolean b = orderService.updateOrder(update);
        if (b && orderDO.getCouponAmount().compareTo(BigDecimal.ZERO) > 0)
            orderConsumer.backCoupon(orderDO.getOrderId());
        return b;
    }
}
